<div style="width:750px; float:left; margin-right:5px" class="border_radius">
<?php
	include_once("../../coms/xl_tintuc.php");
	include_once("../../include/ckeditor/ckeditor.php");
?>
<h3> <span style="color:#006"> Thêm kiến thức</h3>
<table width="100%" cellspacing="0" style="padding-left:5px; padding-right:5px">
	<tr>
    	<td>
        	 <form name="fr_sua" method="post" action="trangchu.php?key=t_qlkt" enctype="multipart/form-data">
                <table width="98%" align="center" cellpadding="5px" cellspacing="20px">
                    <tr>
                        <td width="20%">Tiêu đề kiến thức &nbsp;</td>
                        <td><input type="text" name="txt_tieude" value="" size="50"/></td>
                    </tr>
                    <tr>
                        <td width="20%">Từ khóa &nbsp;</td>
                        <td><input type="text" name="txt_tukhoa" value="" size="50"/></td>
                    </tr>
                    <tr>
                        <td>Chọn hình &nbsp;</td>
                        <td><input type="file" name="up_hinh" /></td>
                    </tr>
                    <tr>
                        <td colspan="2">Nội dung thu gọn</td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <?php 
                                $CKEditor = new CKEditor();
                                $CKEditor->basePath = '../../include/ckeditor/';
                                $CKEditor->config['height'] = 100;
                                $CKEditor->config['width'] =680;
                                $CKEditor->editor("noidung_thugon","");  
                            ?>	      
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">Nội dung đầy đủ</td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <?php 
                                $CKEditor_new = new CKEditor();
                                $CKEditor_new->basePath = '../../include/ckeditor/';
                                $CKEditor_new->config['height'] = 500;
                                $CKEditor_new->config['width'] =680;
                                $CKEditor_new->editor("noidung_daydu","");  
                            ?>	      
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center">
                            <input type="submit" name="bt_capnhat" value="Thêm mới" class="button small blue"/>
                            <input type="button" name="bt_lamlai" value="Làm lại"  onClick="javascript:window.location='trangchu.php?key=t_qlkt'" class="button small blue"/>
                        </td>
                    </tr>
                </table>
            </form>
        </td>
    </tr>
</table>
</div>
<?php
 
if(isset($_POST['bt_capnhat']))
{
	$tieude = $_POST['txt_tieude'];
	$tukhoa = $_POST['txt_tukhoa'];
	$file = $_FILES['up_hinh']['name'];
	$noidungthugon = $_POST['noidung_thugon'];
	$noidungdaydu = $_POST['noidung_daydu'];
	$manguoi = $_SESSION['TenDN'];
		

	$now = getdate();
	$currentDate = $now["year"] . "-" . $now["mon"] . "-" . $now["mday"]; 
	$ngaydang = $currentDate;
	
	echo $ngaydang;
	$kq=array(0);
	$them = new xl_tintuc();
	if($file != NULL)
	{
		$kq=up_hinh();

		if($kq[2]==true)
		{
			$now = getdate();
			$ten = "images/tintuc/".$kq[1];
			$ketqua = $them->them_kienthuc($tieude,$noidungthugon,$noidungdaydu,$tukhoa,$ten,$ngaydang,$manguoi);
			echo '<meta http-equiv="refresh" content="0;url=trangchu.php?key=qlkt"  />';
		}
	}
	else
	{
		$now = getdate();
		$ketqua = $them->them_kienthuc1($tieude,$noidungthugon,$noidungdaydu,$tukhoa,$ngaydang,$manguoi);
		echo '<meta http-equiv="refresh" content="0;url=trangchu.php?key=qlkt"  />';
	}
	
}

function up_hinh()
{
	$result = "";
	define('UPLOAD_DIR','../../images/tintuc/');
	define('MAX_FILES_SIZE',10000000);
	$file = str_replace(" ","_",$_FILES['up_hinh']['name']);
	//convert maximun size to KB
	$max = number_format(MAX_FILES_SIZE/1024,1)." KB";
	//create an array of permitted MIME types
	$premitted = array('image/gif','image/jpeg','image/pjpeg','image/png');
	//begin by assuming the file is unacceptable
	$sizeOK = false;
	$typeOK = false;
	$success = false;
	$kq=array(3);
	//Check that file is within the permitted size
	if($_FILES['up_hinh']['size']>0 && $_FILES['up_hinh']['size']<= MAX_FILES_SIZE)
	{
		$sizeOK = true;
	}
	
	//Check that file is within the permitted size
	foreach($premitted as $type)
	{
		if($type == $_FILES['up_hinh']['type'])
		{
			$typeOK = true;
			break;
		}
	}
	
	if($sizeOK && $typeOK)
	{
		switch($_FILES['up_hinh']['error'])
		{
			case 0:
				if(!file_exists(UPLOAD_DIR.$file))
				{			
					$success = move_uploaded_file($_FILES['up_hinh']['tmp_name'],UPLOAD_DIR.$file);
					$kq[1]=$file;
					$kq[0] = UPLOAD_DIR;
					
				}
				else
				{
					$now  = date("YmdHis",time());
					$file = $now.$file;
					$success = move_uploaded_file($_FILES['up_hinh']['tmp_name'],UPLOAD_DIR.$file);
					$kq[1]=$file;
					$kq[0] = UPLOAD_DIR;
				}
				if($success){
					$result = "$file upload thành công";
					//$result = "$file uploaded successfully";
				}
				else{
					$result = "Lỗi upload $file.Vui long thử lại.";
					//$result = "Error uploading $file.Please try again.";
				}
				break;
			case 3:
				$result = "Lỗi upload $file.Vui long thử lại.";
				//$result = "Error uploading $file.Please try again.";
			default:
				$result = "Lỗi hệ thống upload $file.Vui long thử lại.";
				//$result = "System error uploadding $file contact webmaster";
		}
	}
	elseif($_FILES['up_hinh']['error']==4){
		$result = "Chưa chọn file upload";
		//$result = "No file selected";
	}
	else{
		$result = "Lỗi file upload. Maximum size: $max. File upload phải là gif, jpg, png.";
		//$result ="$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png.";
	}
	if($success == TRUE)
	{
		echo '<script language="javascript">
				window.alert("'.$result.'");
			</script>';
		//echo '<meta http-equiv="refresh" content="2;url=them_tintuc.php"  />';
	}
	else
	{
		echo '<script language="javascript">
				window.alert("'.$result.'");
			</script>';
		//echo '<meta http-equiv="refresh" content="2;url=them_tintuc.php"  />';
	}
	$kq[2] = $success;
	return $kq;
}

		
?>
